/*
Example
----------------------
var pg = new showPages('pg');
pg.pageCount = 12; //定义总页数(必要)
pg.argName = 'p';    //定义参数名(可选,缺省为page)
pg.printHtml();        //显示页数

Supported in Internet Explorer, Mozilla Firefox
*/
function showPages(name) { //初始化属性
	this.name = name;      //对象名称
	this.page = 1;         //当前页数
	this.pageCount = 0;    //总页数
	this.itemCount = 99;    //数据条数
	this.pageSize = 10;    //每页显示条数
	this.argName = 'page'; //参数名
	this.showTimes = 1;    //打印次数
	
	this.firstPageText = "首页";  //首页显示文字
	this.lastPageText = "末页";   //尾页显示文字
	this.prevPageText = "上页"; //上一页显示文字
	this.nextPageText = "下页"; //下一页显示文字
	this.displaynum = 9;  //中间显示页数，保持对称 奇数有效，偶数自动减一    模式3有效
}

showPages.prototype.checkPages = function(){ //进行当前页数和总页数的验证
	if (isNaN(parseInt(this.page))) this.page = 1;
	if (this.page < 1) this.page = 1;
	if (isNaN(parseInt(this.itemCount))) this.itemCount = 1;
	//计算总页码
    if(!this.pageCount) this.pageCount = (this.itemCount/this.pageSize <= 0) ? 1 : (parseInt(this.itemCount/this.pageSize) + ((this.itemCount%this.pageSize>0) ? 1 : 0));		
	if (isNaN(parseInt(this.pageCount))) this.pageCount = 1;
	if (this.pageCount < 1) this.pageCount = 1;
	if (this.page > this.pageCount) this.page = this.pageCount;
	this.page = parseInt(this.page);
	this.pageCount = parseInt(this.pageCount);
}
showPages.prototype.formatInputPage = function(e){ //限定输入页数格式
	var ie = navigator.appName=="Microsoft Internet Explorer"?true:false;
	if(!ie) var key = e.which;
	else var key = event.keyCode;
	if (key == 8 || key == 46 || (key >= 48 && key <= 57)) return true;
	return false;
}
showPages.prototype.createHtml = function(mode){ //生成html代码
	var strHtml = '', prevPage = this.page - 1, nextPage = this.page + 1;
//	strHtml += '<span class="count">显示第'+(this.pageSize * (this.page - 1)+1)+' - '+ (Math.min(this.pageSize * this.page, this.itemCount)) +'条记录, 共'+this.itemCount+'条记录</span>';
	strHtml += '<span class="number">';
	if (prevPage < 1) {
		strHtml += this.firstPageText;
		strHtml += " ";
		strHtml += this.prevPageText;
	} else {
		strHtml += '<span title="First Page"><a href="javascript:' + this.name + '.toPage(1);">'+this.firstPageText+'</a></span>';
		strHtml += " ";
		strHtml += '<span title="Prev Page"><a href="javascript:' + this.name + '.toPage(' + prevPage + ');">'+this.prevPageText+'</a></span>';
	}
//	if (this.page != 1) strHtml += '<span title="Page 1"><a href="javascript:' + this.name + '.toPage(1);">1</a></span>';
//	if (this.page >= 5) strHtml += '<span>...</span>';
//	if (this.pageCount > this.page + parseInt(this.displaynum/2)) {
//		var endPage = this.page + parseInt(this.displaynum/2);
//	} else {
//		var endPage = this.pageCount;
//	}
//	for (var i = this.page - parseInt(this.displaynum/2); i <= endPage; i++) {
//		if (i > 0) {
//			if (i == this.page) {
//				strHtml += '<span title="Page ' + i + '" class="current">' + i + '</span>';
//			} else {
//				if (i != 1 && i != this.pageCount) {
//					strHtml += '<span title="Page ' + i + '"><a href="javascript:' + this.name + '.toPage(' + i + ');">' + i + '</a></span>';
//				}
//			}
//		}
//	}
//	if (this.page + 3 < this.pageCount) strHtml += '<span>...</span>';
//	if (this.page != this.pageCount) strHtml += '<span title="Page ' + this.pageCount + '"><a href="javascript:' + this.name + '.toPage(' + this.pageCount + ');">' + this.pageCount + '</a></span>';
	
	strHtml += " ";
	
	if (nextPage > this.pageCount) {
		strHtml += this.nextPageText;
		strHtml += " ";
		strHtml += this.lastPageText;
	} else {
		strHtml += '<span title="Next Page"><a href="javascript:' + this.name + '.toPage(' + nextPage + ');">'+this.nextPageText+'</a></span>';
		strHtml += " ";
		strHtml += '<span title="Last Page"><a href="javascript:' + this.name + '.toPage(' + this.pageCount + ');">'+this.lastPageText+'</a></span>';
	}
	strHtml += " ";
	
	switch (mode) {
		case 1 :
			strHtml += '跳转到<input type="text" autocomplete="off" id="pageInput' + this.showTimes + '" value="' + this.page + '" class="pagenum" title="Input page" onkeypress="return ' + this.name + '.formatInputPage(event);" onfocus="this.select()">页 <a href="#this" onclick="' + this.name + '.toPage(document.getElementById(\'pageInput' + this.showTimes + '\').value);" class="gotoPage">Go</a>';
			break;
		case 2 :
			if (this.pageCount < 1) {
				strHtml += '<select name="toPage" disabled>';
				strHtml += '<option value="0">No Pages</option>';
			} else {
				var chkSelect;
				strHtml += '<select name="toPage" onchange="' + this.name + '.toPage(this);">';
				for (var i = 1; i <= this.pageCount; i++) {
					if (this.page == i) chkSelect=' selected="selected"';
					else chkSelect='';
					strHtml += '<option value="' + i + '"' + chkSelect + '>' + i + ' / ' + this.pageCount + '</option>';
				}
			}
			strHtml += '</select>';
			break;
		default :
			strHtml = 'Javascript showPage Error: not find mode ' + mode;
			break;
	}
	
	strHtml += '</span><br />';
	return strHtml;
}

showPages.prototype.toPage = function(page){ //页面跳转
	var turnTo = 1;
	if (typeof(page) == 'object') {
		turnTo = page.options[page.selectedIndex].value;
	} else {
		turnTo = page;
	}
	var regex = /^\d+$/;
	if(!regex.test(turnTo)){
		alert("请输入正确的页码");
		return ;
	}
	document.getElementById(this.argName).value = turnTo;
	document.forms[0].submit();
}
showPages.prototype.printHtml = function(mode){ //显示html代码
	this.checkPages();
	this.showTimes += 1;
	document.write('<div id="pages_' + this.name + '_' + this.showTimes + '" class=""></div>');
	document.getElementById('pages_' + this.name + '_' + this.showTimes).innerHTML = this.createHtml(mode);
	
}
